# -*- coding: utf-8 -*-

import time
import csv
from StatisticsHolder import *

class GraphsGenerator:
    def __init__(self):
##        self.generateHigh()
##        self.generateCategory()
##        self.generateDegree()
        pass

    def generateHigh(self):
        t1 = time.clock()

        # Insert GraphGenerationCode here

        self.generateHighCSV()
        
        t2 = time.clock()
        print "Graph High =", t2-t1
        pass

    def generateHighCSV(self):
        t1 = time.clock()

        holder = StatisticsHolder()

        stats = holder.countHighTeachers()

        writer = csv.writer(open("./csv/CountHighTeachers.csv", "wb"))

        writer.writerow(["Year", "Establishment", "Number of teachers"])

        for year in sorted(stats.iterkeys()):
            for est in sorted(stats.get(year).iterkeys()):
                writer.writerow([year.encode("iso-8859-1"), est.encode("iso-8859-1"),\
                                 stats.get(year).get(est)])
                pass
            pass
        
        t2 = time.clock()
        print "Graph High CSV =", t2-t1
        pass

    def generateCategory(self):
        t1 = time.clock()

        # Insert GraphGenerationCode here

        self.generateCategoryCSV()

        t2 = time.clock()
        print "Graph Category =", t2-t1
        pass

    def generateCategoryCSV(self):
        t1 = time.clock()

        holder = StatisticsHolder()

        stats = holder.countCategoryTeachers()

        writer = csv.writer(open("./csv/CountCategoryTeachers.csv", "wb"))

        writer.writerow(["Year", "Establishment", "Category",\
                         "Number of teachers"])

        for year in sorted(stats.iterkeys()):
            for est in sorted(stats.get(year).iterkeys()):
                for cat in sorted(stats.get(year).get(est).iterkeys()):
                    writer.writerow([year.encode("iso-8859-1"),\
                                     est.encode("iso-8859-1"),\
                                     cat.encode("iso-8859-1"),\
                                     stats.get(year).get(est).get(cat)])
                    pass
                pass
            pass

        t2 = time.clock()
        print "Graph Category CSV =", t2-t1
        pass

    def generateDegree(self):
        t1 = time.clock()

        # Insert GraphGenerationCode here

        self.generateDegreeCSV()

        t2 = time.clock()
        print "Graph Degree =", t2-t1
        pass

    def generateDegreeCSV(self):
        t1 = time.clock()

        holder = StatisticsHolder()

        stats = holder.countDegreeTeachers()

        writer = csv.writer(open("./csv/CountDegreeTeachers.csv", "wb"))

        writer.writerow(["Year", "Establishment", "Degree",\
                         "Number of teachers"])

        for year in sorted(stats.iterkeys()):
            for est in sorted(stats.get(year).iterkeys()):
                for deg in sorted(stats.get(year).get(est).iterkeys()):
                    writer.writerow([year.encode("iso-8859-1"),\
                                     est.encode("iso-8859-1"),\
                                     deg.encode("iso-8859-1"),\
                                     stats.get(year).get(est).get(deg)])
                    pass
                pass
            pass

        t2 = time.clock()
        print "Graph Degree CSV =", t2-t1
        pass

##graphs = GraphsGenerator()
